// 修改后的request.ts
import axios, { AxiosInstance } from 'axios';

// 类型断言声明环境类型
declare const process: NodeJS.Process & {
  env: {
    VUE_APP_BASE_API?: string;
  };
};

const service: AxiosInstance = axios.create({
  baseURL: process.env.VUE_APP_BASE_API || '', // 添加默认值
  timeout: 5000,
  headers: { 'Content-Type': 'application/json' }
});

// 添加类型定义
interface ApiResponse<T> {
  code: string;
  data: T;
  message: string;
}

service.interceptors.response.use(
  response => {
    const res = response.data as ApiResponse<any>;
    if (res.code !== '-1' && res.code !== '0') {
      console.error(res.message);
      return Promise.reject(new Error(res.message || 'Error'));
    }
    return response;
  },
  error => {
    console.error('请求异常:', error);
    return Promise.reject(error);
  }
);

export default service;